import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chubao/tools/app_tool_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_chubao/binging/app_getx_binging.dart';
import 'package:flutter_chubao/config/app_config.dart';
import 'package:flutter_chubao/pages/main/app_main/app_main_view.dart';
import 'package:flutter_chubao/route/app_route_name.dart';
import 'package:flutter_chubao/route/app_routes.dart';
import 'package:flutter_chubao/tools/app_log.dart';
import 'package:flutter_chubao/tools/app_tool_toast.dart';
import 'package:flutter_chubao/tools/app_textstyle.dart';
import 'package:flutter_chubao/tools/shared/app_shared.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_client.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_config.dart';
import 'package:flutter_chubao/widgets/app_button_styles.dart';
import 'package:flutter_chubao/widgets/app_widget_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// import 'package:flutter_unionad/flutter_unionad.dart';
import 'package:get/get.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (GetPlatform.isAndroid) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.dark,
        statusBarIconBrightness: Brightness.dark));
  } else {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
  }

  AppToolToast.setToastStyle();
  AppDioConfig dioConfig = AppDioConfig(
      baseUrl: AppConfig.baseHost,
      sendTimeout: AppConfig.netTimeOut,
      connectTimeout: AppConfig.netTimeOut,
      receiveTimeout: AppConfig.netTimeOut,
      headers: {
        "Accept": "Application/json",
        "Content-Type": "Application/json",
        "platform": GetPlatform.isAndroid ? 'android' : "ios",
      });
  if (GetPlatform.isAndroid) {
    AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true)
        .then((value) async {
      var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
          AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
      var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
          AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);

      if (swAvailable && swInterceptAvailable) {
        AndroidServiceWorkerController serviceWorkerController =
            AndroidServiceWorkerController.instance();
        await serviceWorkerController
            .setServiceWorkerClient(AndroidServiceWorkerClient(
          shouldInterceptRequest: (request) async {
            DLog(request);
            return null;
          },
        ));
      }
    });
  }

  AppSharedPro.sharedPreferences = await SharedPreferences.getInstance();
  AppDioClient.initDio(dioConfig: dioConfig);
  // if (AppSharedPro.getString(AppShareConstant.deviceId).isEmpty) {
  //   String deviceId = const Uuid().v1();
  //   AppConfig.deviceId = deviceId;
  //   AppSharedPro.setString(AppShareConstant.deviceId, deviceId);
  // } else {
  //   AppConfig.deviceId = AppSharedPro.getString(AppShareConstant.deviceId);
  // }
// 逐梦电竞5477562
  // await getPrivacy();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return AppWidgetRefresh(
            child: GetMaterialApp(
          debugShowCheckedModeBanner: false,
          initialRoute: AppRouteName.pageInitial,
          initialBinding: AppGetxBinding(),
          getPages: AppRoutes.pages,
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            GlobalWidgetsLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
            RefreshLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('zh', 'CN'),
          ],
          defaultTransition: Transition.native,
          home: AppMainPage(),
          routingCallback: (routing) {
            DLog('=======main:${routing?.current}');
            if (routing?.current == '/second') {
              // 如果登录。。。
            }
          },
          builder: EasyLoading.init(),
          title: '厨宝',
          theme: ThemeData(
            scaffoldBackgroundColor: Colors.white,
            appBarTheme: AppBarTheme(
              centerTitle: true,
              elevation: 0.0,
              titleTextStyle: AppTextStyle.getBoldTextStyle(fontSize: 16),
            ),
            brightness: Brightness.light,
            elevatedButtonTheme: ElevatedButtonThemeData(
                style: MyButtonStyles.elevatedButtonStyle),
            outlinedButtonTheme: OutlinedButtonThemeData(
                style: MyButtonStyles.outlinedButtonStyle),
            textTheme: const TextTheme(
              displayMedium:
                  TextStyle(fontSize: 46.0, fontWeight: FontWeight.bold),
              titleMedium:
                  TextStyle(fontSize: 16.0, fontStyle: FontStyle.italic),
              bodyMedium: TextStyle(
                  fontSize: 16.0,
                  fontStyle: FontStyle.normal,
                  color: Colors.white),
              labelSmall: TextStyle(
                  fontSize: 16.0,
                  fontStyle: FontStyle.normal,
                  color: Colors.white),
            ),
          ),
        ));
      },
    );
  }
}
